package com.peak.distribution.web.order;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.peak.distribution.entity.agent.DisAgent;
import com.peak.distribution.entity.order.DisOrderReceiver;
import com.peak.distribution.service.agent.DisAgentService;
import com.peak.distribution.vo.order.DisOrderReceiverVo;
import com.peak.spring.boot.api.constant.ErrorMessage;
import com.peak.spring.boot.api.constant.SuccessMessage;
import com.peak.spring.boot.api.repository.FilterParam;
import com.peak.spring.boot.api.repository.FilterParam.Operator;
import com.peak.spring.boot.api.web.JsonMessage;
import com.peak.spring.boot.api.web.JsonPage;
import com.peak.spring.boot.builder.repository.FilterParamBuilder;
import com.peak.spring.boot.builder.web.JsonBuilder;
import com.peak.spring.boot.core.exception.ProjectException;
import com.peak.spring.boot.core.web.BaseController;
import com.peak.spring.boot.tools.Emptys;

@RestController
@RequestMapping("/order/reveiver")
public class DisOrderReceiverController extends BaseController<DisOrderReceiver, DisOrderReceiverVo> {

	@Autowired
	private DisAgentService agentService;

	@PostMapping("/setDefault/{id}")
	public JsonMessage setDefault(@PathVariable("id") Integer id) {
		// 取出要设置为默认的记录
		DisOrderReceiver receiver = baseManager.findOne(id);
		if (receiver == null) {
			throw new ProjectException(ErrorMessage.DATA_NOT_FOUND);
		}
		// 取出以前设置的默认地址
		List<DisOrderReceiver> receivers = baseManager.findList(FilterParamBuilder.create().eq("isDefault", true).eq("agent.id", receiver.getAgent().getId()).build());
		if (Emptys.isNotEmpty(receivers)) {// 循环把以前的默认改为非默认
			receivers.stream().forEach(p -> p.setIsDefault(Boolean.FALSE));
		}
		// 设置当前的记录为默认
		receiver.setIsDefault(Boolean.TRUE);
		baseManager.save(receivers);
		baseManager.save(receiver);
		return JsonBuilder.message().success().content(SuccessMessage.SUCCESS).build();
	}
	
	@PostMapping("/current/list")
	public JsonPage list(HttpServletRequest request) {
		FilterParamBuilder builder = FilterParamBuilder.create().parseRequest(request);
		List<FilterParam> filterParams = builder.build();
		filterParams.add(new FilterParam("agent.id", Operator.EQ, agentService.getCurrentAgentId()));
		PageRequest pageRequest = builder.pageRequest();
		beforeList(request, filterParams, pageRequest);
		Page<DisOrderReceiver> page = baseManager.findPage(filterParams, pageRequest);
		JsonPage json = JsonBuilder.page().success().page(page).convert(getVoClass()).build();
		json = afterList(json);
		return json;
	}

	@Override
	protected DisOrderReceiver beforeSave(HttpServletRequest request, DisOrderReceiver entity) {
		// 取出当前登录的代理商
		DisAgent agent = agentService.getCurrentAgent();
		entity.setAgent(agent);
		if (Emptys.isEmpty(entity.getId())) {// 如果是新增
			List<DisOrderReceiver> receivers = baseManager.findList(FilterParamBuilder.create().eq("isDefault", true).eq("agent.id", agent.getId()).build());
			if (Emptys.isEmpty(receivers)) {// 如果没有其他记录
				// 第一条记录为默认地址
				entity.setIsDefault(Boolean.TRUE);
			}
		}
		return super.beforeSave(request, entity);
	}

	@Override
	protected void beforeDelete(DisOrderReceiver entity) {
		if (entity.getIsDefault()) {// 如果是删除默认地址
			// 查询不是当前要删除的记录的其他当前代理商的记录
			List<DisOrderReceiver> receivers = baseManager.findList(FilterParamBuilder.create().eq("agent.id", agentService.getCurrentAgentId()).neq("id", entity.getId()).build());
			if (Emptys.isNotEmpty(receivers)) {// 如果有记录
				// 第一条记录为默认地址
				DisOrderReceiver first = receivers.get(0);
				first.setIsDefault(Boolean.TRUE);
				baseManager.save(first);
			}
			// 因为是逻辑删除，真实数据不会被删除，所以把默认的地址设置为普通
			entity.setIsDefault(Boolean.FALSE);
		}
		super.beforeDelete(entity);
	}
}
